<?php


namespace app\admin\controller;


use app\BaseController;
use app\middleware\admin\Auth;
use app\model\Product as ProductModel;
use app\model\ProductSku;
use think\facade\Db;

class Product extends BaseController
{
    protected $middleware=[Auth::class];
    public function index()
    {
        $keyword=request()->param('keyword');

        $data=ProductModel::when($keyword,function ($query) use($keyword){
            $query->where('name','like',"%{$keyword}%")
                ->whereOr('borcode','like',"%{$keyword}%");
        })->with('cate')
        ->order('id','desc')->paginate(10)->toArray();

        $remind=\app\model\Store::where('id','>',0)->value('remind');

        if(!empty($data)){
            foreach ($data['data'] as &$item){
                $item['material_cost']=explode(',',$item['material_cost']);
                if($item['num']<=$remind && $item['type']==2){
                    $item['inform']=1;
                }else{
                    $item['inform']=0;
                }

                if($item['type']==1){
                    $item['num']='-';
                }
            }
        }

        return $this->success($data);
    }


    public function create()
    {
        $params=request()->param();

        if (!empty($params['place'])) {
            $str = $params['name'].'的位置在'.$params['place'];
            Db::table('admin_log')->insert([
                'type'=>4,
                'admin_id'=>request()->userid,
                'content'=>$str,
                'created_at'=>time()
            ]);
        }

        if (!empty($params['cost'])) {
            $str = $params['name'].'的成本是'.$params['cost'];
            Db::table('admin_log')->insert([
                'type'=>4,
                'admin_id'=>request()->userid,
                'content'=>$str,
                'created_at'=>time()
            ]);
        }
        return $this->success(ProductModel::create(request()->param()));
    }


    public function update($id)
    {
        $params=request()->param();


        if(!empty($params['productsku'])){
            foreach ($params['productsku'] as $item){

                $res['specs_value_id']=implode('_',array_column($item['name'],'id'));
                $res['products_id']=$item['products_id'];
                $res['price']=$item['price'];
                $res['const']=$item['const'];
                $res['num']=$item['num'];
                $data[]=$res;
            }
            ProductSku::where('products_id',$id)->delete();

            ProductSku::insertAll($data);
        }

        $goods=\app\model\Product::where('id',$id)->find();
            if($params['type']==2){
                if (!empty($params['num']) && $goods['num']!=$params['num']) {
                    $num =  $params['num']-$goods['num'] ;
                    if($num>0){
                        $str = $goods['name'].'的庫存加'.$num;
                    }else{
                        $str = $goods['name'].'的庫存減'.$num;
                    }

                    Db::table('admin_log')->insert([
                        'type'=>1,
                        'admin_id'=>request()->userid,
                        'content'=>$str,
                        'created_at'=>time()
                    ]);
                }
            }

        if (!empty($params['place'])) {
            $str = $goods['name'].'的位置在'.$params['place'];
            Db::table('admin_log')->insert([
                'type'=>4,
                'admin_id'=>request()->userid,
                'content'=>$str,
                'created_at'=>time()
            ]);
        }
        if (!empty($params['cost'])) {
            $str = $params['name'].'的成本是'.$params['cost'];
            Db::table('admin_log')->insert([
                'type'=>4,
                'admin_id'=>request()->userid,
                'content'=>$str,
                'created_at'=>time()
            ]);
        }

        unset($params['productsku']);
      
        return $this->success(ProductModel::where('id',$id)->update($params));
    }


    public function show($id)
    {
        $data=\app\model\Product::with(['cate','sku','specs.spec'])->where('id',$id)->find()->toArray();
        if(!empty($data['sku'])){
            foreach ($data['sku'] as &$item){
              $name= \app\model\SpecsValue::where('id','in',explode('_',$item['specs_value_id']))->select()->toArray() ;
                $item['name']=$name;
            }

        }
        return $this->success($data);
    }

    public function delete($id)
    {
        return $this->success(ProductModel::where('id',$id)->delete());
    }
}